package com.demo.jpademo.service;

import com.demo.jpademo.dao.MyUserDao;
import com.demo.jpademo.pojo.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/*
* myuser业务层
* */
@Service
public class MyUserService {
    @Autowired
    private MyUserDao myUserDao;

    /*
    * 查询所有
    * */
    public List<MyUser> findAll(){
        return this.myUserDao.findAll();
    }
    /*
    * 根据id查询所有
    * */
    public Optional<MyUser> findById(String id){
//        不等于空的情况下再查询否则返回空
        if (StringUtils.isEmpty(id)){
        return null;
        }else {
        return this.myUserDao.findById(Integer.valueOf(id));
        }
    }
    /*
    * 添加
    * */
    public MyUser addMyUser(MyUser myUser){
        if (ObjectUtils.isEmpty(myUser)){
            return null;
        }else {
        return this.myUserDao.save(myUser);
        }
    }
    /*
    * 根据ID删除
    * */
    public int delById(String id){
        if (StringUtils.isEmpty(id)){
            return 0;
        }else {
            this.myUserDao.deleteById(Integer.valueOf(id));
            System.out.println(id);
            return 1;
        }
    }
    /*
    * 根据ID进行更新
    * */
    public MyUser upById(MyUser myUser){
        if (ObjectUtils.isEmpty(myUser)){
            return null;
        }else { return this.myUserDao.save(myUser);}
    }
}
